library(patchwork)
library(plyr)
library(dplyr)
library(ggplot2)

# set to desired working directory
# setwd("")

# Start log file for script -----------------------------------------------
TeachingDemos::txtStart("./log files/figureA24.txt")

# Load Data ---------------------------------------------------------
# individual data
load("./data/impdat1920.Race.WeekWeight.rdata")

# Get Counts ---------------------------------------------------------------
df <- impdat1920.WeekWeight

d_sex <- ddply(df, 
           .(week), 
           count, 
           sex = sex)
dd_sex <- reshape::melt(d_sex, id = c("week", "n"))

d_age <- ddply(df, 
               .(week), 
               count, 
               age = age_cat4)
dd_age <- reshape::melt(d_age, id = c("week", "n"))

d_educ <- ddply(df, 
               .(week), 
               count, 
               educ = edu_cat4)
dd_educ <- reshape::melt(d_educ, id = c("week", "n"))

d_race <- ddply(df, 
               .(week), 
               count, 
               race = race5)
dd_race <- reshape::melt(d_race, id = c("week", "n"))

d_region <- ddply(df, 
               .(week), 
               count, 
               region = census_region)
dd_region <- reshape::melt(d_region, id = c("week", "n"))

d_why <- ddply(df, 
                  .(week), 
                  count, 
                  why = broughtwebsite2)
dd_why <- reshape::melt(d_why, id = c("week", "n"))

d_ideo <- ddply(df, 
                  .(week), 
                  count, 
                  ideo = ideo3)
dd_ideo <- reshape::melt(d_ideo, id = c("week", "n"))
dd_ideo$value <- factor(dd_ideo$value,
                        labels = c("lib", "mod", "con"))

dd_counts <- rbind(dd_sex, dd_age, dd_educ,
                   dd_race, dd_region, dd_why, dd_ideo)
dd_counts$prop <- NA

# removing missing variable values
dd_counts <- subset(dd_counts, !is.na(value))

# Looping to get proportions
weeks <- unique(dd_counts$week)
vars <- unique(dd_counts$variable)
for(i in 1:length(weeks)){
  for(j in 1:length(vars)){
    cell_n <- dd_counts$n[which(dd_counts$variable == vars[j] & dd_counts$week == weeks[i])]
    tot_n <- sum(cell_n)
    dd_counts$prop[which(dd_counts$variable == vars[j] & dd_counts$week == weeks[i])] <- cell_n/tot_n
  }
}

dd_counts$week <- as.numeric(dd_counts$week)


# Checking for Empty Counts -----------------------------------------------
## Checking for 0 weeks [none]
dd_counts[which(dd_counts$prop < .01),]


# Patchwork ---------------------------------------------------------------
p_sex <- ggplot(subset(dd_counts, variable == "sex"),
       aes(x = week, y = prop, group = value)) +
  geom_line(aes(color = value)) +
  geom_vline(xintercept = 21, linetype = "dashed") +
  labs(x = "", y = "Proportion", title = "Sex") +
  theme_bw() +
  scale_x_continuous(breaks = seq(from = 0, to = 52, by = 4)) +
  theme(legend.position = "bottom",
        legend.title = element_blank())

p_age <- ggplot(subset(dd_counts, variable == "age"),
                aes(x = week, y = prop, group = value)) +
  geom_line(aes(color = value)) +
  geom_vline(xintercept = 21, linetype = "dashed") +
  labs(x = "", y = "Proportion", title = "Age") +
  theme_bw() +
  scale_x_continuous(breaks = seq(from = 0, to = 52, by = 4)) +
  theme(legend.position = "bottom",
        legend.title = element_blank())

p_educ <- ggplot(subset(dd_counts, variable == "educ"),
                aes(x = week, y = prop, group = value)) +
  geom_line(aes(color = value)) +
  geom_vline(xintercept = 21, linetype = "dashed") +
  labs(x = "", y = "Proportion", title = "Education") +
  theme_bw() +
  scale_x_continuous(breaks = seq(from = 0, to = 52, by = 4)) +
  guides(colour = guide_legend(nrow = 2)) + 
  theme(legend.position = "bottom",
        legend.title = element_blank())

p_race <- ggplot(subset(dd_counts, variable == "race"),
                aes(x = week, y = prop, group = value)) +
  geom_line(aes(color = value)) +
  geom_vline(xintercept = 21, linetype = "dashed") +
  labs(x = "", y = "Proportion", title = "Race") +
  theme_bw() +
  scale_x_continuous(breaks = seq(from = 0, to = 52, by = 4)) +
  guides(colour = guide_legend(nrow = 2)) + 
  theme(legend.position = "bottom",
        legend.title = element_blank())

p_region <- ggplot(subset(dd_counts, variable == "region"),
                 aes(x = week, y = prop, group = value)) +
  geom_line(aes(color = value)) +
  geom_vline(xintercept = 21, linetype = "dashed") +
  labs(x = "", y = "Proportion", title = "Region") +
  theme_bw() +
  scale_x_continuous(breaks = seq(from = 0, to = 52, by = 4)) +
  guides(colour = guide_legend(nrow = 2)) + 
  theme(legend.position = "bottom",
        legend.title = element_blank())

p_why <- ggplot(subset(dd_counts, variable == "why"), 
                 aes(x = week, y = prop, group = value)) +
  geom_line(aes(color = value)) +
  geom_vline(xintercept = 21, linetype = "dashed") +
  labs(x = "", y = "Proportion", title = "Reason for Visiting") +
  theme_bw() +
  scale_x_continuous(breaks = seq(from = 0, to = 52, by = 4)) +
  guides(colour = guide_legend(nrow = 2)) + 
  theme(legend.position = "bottom",
        legend.title = element_blank())

p_ideo <- ggplot(subset(dd_counts, variable == "ideo"), 
                aes(x = week, y = prop, group = value)) +
  geom_line(aes(color = value)) +
  geom_vline(xintercept = 21, linetype = "dashed") +
  labs(x = "", y = "Proportion", title = "Ideology") +
  theme_bw() +
  scale_x_continuous(breaks = seq(from = 0, to = 52, by = 4)) +
  theme(legend.position = "bottom",
        legend.title = element_blank())


(p_sex + p_age + p_educ + p_race + p_region + p_why + p_ideo) +
  plot_layout(ncol = 2)
file <- paste0("./Figures/FigureA24.pdf")
ggsave(file, height = 10, width = 9)


# End log file for script -------------------------------------------------
TeachingDemos::txtStop()